<template>
  <div class="login">
    <div class="login-card">
      <h1>vue-shop</h1>
      <p>vue-shop 专为vue提供商城后台模板</p>
      <!-- ref vue指令  用于获取组件实例 -->
      <el-form :model="formInput" :rules="rules" ref="loginForm" class="login-form">
        <!-- prop指向验证规则 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
            v-model="formInput.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            v-model="formInput.password" />
        </el-form-item>
        <el-form-item>
          <div class="btn-form">
            <el-button type="primary" @click="submitForm">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { setToken } from '@/utils/js-storage'

export default {
  name: 'login',
  data () {
    return {
      formInput: { // 表单数据
        username: 'admin',
        password: '123456'
      },
      rules: { // 表单验证规则
        username: [
          { required: true, message: '用户名不能未空', trigger: 'blur' },
          { min: 3, max: 12, message: '长度最小为3最大为12', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能未空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () { // 提交表单数据
      // 验证表单项是否满足要求
      this.$refs.loginForm.validate(async vali => {
        if (!vali) return // 不满足要求
        // 请求登录接口
        const { data } = await this.$http.post('login', this.formInput)
        // 判断状态码是否以2开头
        if (String(data.meta.status).startsWith('2')) { // 登录成功
          console.log(data)
          this.$notify({
            title: '登录成功',
            message: '欢迎你，admin，上午好！！！'
          })
          // 登录成功后 将token 存储在 cookie中  安装 js-cookie yarn add js-cookie
          // 在访问其他页面时 从cookie 验证token是否有效
          setToken(data.data.token)
          // 希望每次登录之后  跳转到上次访问的界面
          // 跳转到首页
          this.$router.push({ path: this.$route.query.redirect || '/' })
        } else { // 登录失败
          this.$message.error(data.meta.msg)
        }
      })
    },
    resetForm () { // 重置表单
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('../../assets/antd.svg') no-repeat center 110px;
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-card {
    width: 420px;
    box-sizing: border-box;
    text-align: center;
    h1 {
      color: #303133;
      font-size: 33px;
      font-weight: 400;
      margin: 0;
    }
    p {
      color: #c0c4cc;
      font-size: 14px;
    }
    .login-form {
      margin-top: 70px;
      ::v-deep .el-input {
        .el-input__inner {
          height: 48px;
        }
        .el-input__icon {
          line-height: 48px;
        }
      }
      .btn-form {
        text-align: right;
      }
    }
  }
}
</style>
